package com.gdpu.project.util;

import java.util.Date;
import java.text.SimpleDateFormat;

public class DateUtil {

	/*
	 * date转换成String
	 */
	public static String dateToString(Date date) {
		SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
		if (date != null) {
			return s.format(date);
		}
		return "";
	}

	/*
	 * 将object 的date类型转化为字符串
	 */
	public static String dateToString(Object obj) {
		if (obj == null)
			return "";
		Date date = (Date) obj;
		SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
		if (date != null) {
			return s.format(date);
		}
		return "";
	}

	/*
	 * 将object转化为date数组再取第i个格式化
	 */
	public static String dateToString(Object obj, int i) {
		if (obj == null)
			return "";
		Date[] arrayDate = (Date[]) obj;
		Date date;
		if (0 <= i && i < arrayDate.length) {
			date = arrayDate[i];
		} else {
			return "";
		}

		SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
		if (date != null) {
			return s.format(date);
		}
		return "";
	}

	/**
	 * String转换成date
	 */
	public static Date stringToDate(String date) {
		SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
		Date dDate = null;
		try {
			if (!"".equals(date))
				dDate = (Date) s.parse(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (date != null) {
			return dDate;
		}
		return null;
	}
}
